PHP

How to demonstrate the parameterized constructor in PHP | PHP programs

How to demonstrate the parameterized constructor in PHP | PHP programs

How to demonstrate the parameterized constructor in PHP | PHP programs

We will create a class with data members and implement a parameterized constructor to initialize data members.

<?php  

    class Sample
    {  
        private $num1;
        private $num2;
        
        public function Sample($n1, $n2)
        {  
            $this->num1=$n1;
            $this->num2=$n2;
        }  
        public function PrintValues()
        {  
            echo "Num1: ".$this->num1."<br>";  
            echo "Num2: ".$this->num2."<br>";  
        }  
    }  
    $S = new Sample(10,20);  
    $S->PrintValues();
?> 
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community